package org.example.group5back.service;


import org.example.group5back.entity.MaterialEntity;
import org.example.group5back.mapper.MaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialService {
    @Autowired
    MaterialMapper MaterialMapper;
    public List<MaterialEntity> finAllMaterial(){
        return MaterialMapper.findAllMaterial();
    }
    public boolean saveMaterial(MaterialEntity material){
        System.out.println(material.getMaterial_id());
        List<MaterialEntity> MaterialEntityList = MaterialMapper.findAllMaterial();
        Integer next_material_id = MaterialEntityList.size() + 1;
        System.out.println(next_material_id);
        if(material.getMaterial_id() > 0){
            return MaterialMapper.updateMaterial(material);
        }else{
            material.setMaterial_id(next_material_id);
            return MaterialMapper.insertMaterial(material);
        }
    }
    public boolean deleteMaterial(Integer material_id){
        boolean result = MaterialMapper.deleteMaterial(material_id);
        MaterialMapper.updateAfterDelete(material_id);
        return result;
    }

    public List<MaterialEntity>  selectMaterialByName(String material_name,String material_productiondate,String material_factorysite,String material_type){
        if(material_name == null){
            System.out.println("1");
        }
        return MaterialMapper.selectByName(material_name,material_productiondate,material_factorysite,material_type);
    }
}
